package com.gdpu.watermelontimer.data

import androidx.lifecycle.LiveData
import androidx.room.*
//对 PomodoroSession 表的增删查改操作
@Dao
interface PomodoroSessionDao {
    @Query("SELECT * FROM pomodoro_sessions ORDER BY startTime DESC")
    fun getAllSessions(): LiveData<List<PomodoroSession>>
    
    @Query("SELECT * FROM pomodoro_sessions WHERE taskId = :taskId ORDER BY startTime DESC")
    fun getSessionsByTask(taskId: Long): LiveData<List<PomodoroSession>>
    
    @Query("SELECT * FROM pomodoro_sessions WHERE startTime >= :startTime AND startTime < :endTime")
    fun getSessionsByDateRange(startTime: Long, endTime: Long): LiveData<List<PomodoroSession>>
    
    @Query("SELECT COUNT(*) FROM pomodoro_sessions WHERE sessionType = 'WORK' AND isCompleted = 1 AND startTime >= :startTime AND startTime < :endTime")
    fun getCompletedWorkSessionsCount(startTime: Long, endTime: Long): LiveData<Int>
    
    @Insert
    suspend fun insertSession(session: PomodoroSession): Long
    
    @Update
    suspend fun updateSession(session: PomodoroSession)
    
    @Delete
    suspend fun deleteSession(session: PomodoroSession)
    
    @Query("UPDATE pomodoro_sessions SET endTime = :endTime, actualDuration = :actualDuration, isCompleted = 1 WHERE id = :sessionId")
    suspend fun completeSession(sessionId: Long, endTime: Long, actualDuration: Long)
    
    @Query("UPDATE pomodoro_sessions SET isInterrupted = 1 WHERE id = :sessionId")
    suspend fun markSessionAsInterrupted(sessionId: Long)
} 